package org.icress.fusion.moc

import org.apache.shiro.subject.Subject
import org.apache.shiro.SecurityUtils

import org.icress.fusion.shiro.User

class TgController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        render view : '/moc/tg/list', model : [tgInstanceList: Tg.list(params), tgInstanceTotal: Tg.count()]
    }

    def create = {
        def tgInstance = new Tg()
        tgInstance.properties = params
        render view : '/moc/tg/create', model : [tgInstance: tgInstance]
    }

    def save = {
        def tgInstance = new Tg(params)
		tgInstance.validate()
        if (!tgInstance.hasErrors() && tgInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'moc.tg.label', default: 'Tg'), tgInstance.id])}"
            redirect(controller : "tc", action: "show", id: tgInstance.tc.id)
        }
        else {
            render(view: "/moc/tg/create", model: [tgInstance: tgInstance])
        }
    }

    def show = {
        def tgInstance = Tg.get(params.id)
        if (!tgInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tg.label', default: 'Tg'), params.id])}"
            redirect(action: "list")
        }
        else {
            render view : '/moc/tg/show', model : [tgInstance: tgInstance]
        }
    }

    def edit = {
        def tgInstance = Tg.get(params.id)
        if (!tgInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tg.label', default: 'Tg'), params.id])}"
            redirect(action: "list")
        }
        else {
			Subject currentUser = SecurityUtils.getSubject();
			if(currentUser.hasRole('admin') || currentUser.hasRole('manager') ||currentUser.getPrincipal().equals(tgInstance.tc.owner.username) ){
				render view : '/moc/tg/edit', model : [tgInstance: tgInstance]
			}else{
				render view : '/unauthorized'
			}
            
        }
    }

    def update = {
        def tgInstance = Tg.get(params.id)
        if (tgInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (tgInstance.version > version) {
                    
                    tgInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'moc.tg.label', default: 'Tg')] as Object[], "Another user has updated this Tg while you were editing")
                    render(view: "/moc/tg/edit", model: [tgInstance: tgInstance])
                    return
                }
            }
            tgInstance.properties = params
			tgInstance.validate()
            if (!tgInstance.hasErrors() && tgInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'moc.tg.label', default: 'Tg'), tgInstance.id])}"
                redirect(controller : "tc", action: "show", id: params["tc.id"])
            }
            else {
                render(view: "/moc/tg/edit", model: [tgInstance: tgInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tg.label', default: 'Tg'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def tgInstance = Tg.get(params.id)
        if (tgInstance) {
            try {
                tgInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'moc.tg.label', default: 'Tg'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'moc.tg.label', default: 'Tg'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tg.label', default: 'Tg'), params.id])}"
            redirect(action: "list")
        }
    }
}
